﻿/*********************************************************************
 * Copyright 2012 Fan Deng.
 * 
 * This file is part of uscis-status-tracker.
 * 
 * uscis-status-tracker is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by 
 * the Free Software Foundation, either version 3 of the License, or 
 * (at your option) any later version.
 * 
 * uscis-status-tracker is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *********************************************************************/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace USCISStatusTracker
{
	public partial class Form1 : Form
	{
		private ReceiptPool pool;
		public Form1()
		{
			InitializeComponent();
		}

		private void Form1_Load(object sender, EventArgs e)
		{
			label1.Text =	"Examples:" + System.Environment.NewLine +
							"SRCxxxxxxxxxx" + System.Environment.NewLine +
							"SRCxxxxxxx010-100" + System.Environment.NewLine +
							"The second example shows how to enter a range of receipt numbers.";
			pool = ReceiptPool.FromFile("receipt.pool");
			//pool.AddDummy();
			//pool.ToFile("receipt.pool");
			dataGridView1.DataSource = pool.GetData();
		}

		private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
		{

		}

		private List<string> ProcessReceiptRequest(string receiptNumber)
		{
			if (!receiptNumber.Contains("-"))
			{
				var list = new List<string>();
				list.Add(receiptNumber);
				return list;
			}
			else
			{
				try
				{
					int id = receiptNumber.IndexOf("-");
					string baseNumber = receiptNumber.Substring(0, id);
					string endString = receiptNumber.Substring(id + 1);
					int endNumber = Convert.ToInt32(endString);
					int cnt_endNumber = endString.Length;
					if (cnt_endNumber > id)
					{
						throw new Exception();
					}
					string startString = receiptNumber.Substring(id - cnt_endNumber, cnt_endNumber);
					int startNumber = Convert.ToInt32(startString);
					if (startNumber > endNumber)
					{
						throw new Exception();
					}
					string baseString = receiptNumber.Substring(0, id - cnt_endNumber);
					string zeroString = "";
					var list = new List<string>();
					//MessageBox.Show(baseString);
					//MessageBox.Show(startNumber.ToString());
					//MessageBox.Show(endNumber.ToString());
					for (int i = startNumber; i <= endNumber; i++)
					{
						string appendix = i.ToString();
						int zeroNeeded = id - baseString.Length - appendix.Length;
						if (zeroNeeded == zeroString.Length)
						{
							list.Add(baseString + zeroString + appendix);
						}
						else
						{
							// fix zero string
							zeroString = "";
							for (int j = 0; j < zeroNeeded; j++)
							{
								zeroString += "0";
							}
							list.Add(baseString + zeroString + appendix);
						}
					}
					return list;
				}
				catch
				{
					return null; // failed to parse
				}
			}
		}

		private void button1_Click(object sender, EventArgs e)
		{
			string receiptNumber = textBox1.Text;

			var list = this.ProcessReceiptRequest(receiptNumber);
			const string InvalidInputMessage = "Invalid input.";
			if ((list == null) || (list.Count == 0))
			{
				MessageBox.Show(InvalidInputMessage);
				return;
			}

			this.Cursor = Cursors.WaitCursor;
			BusyBox box = new BusyBox();
			box.Show();
			USCISStatus newItem = USCISStatus.Track(list[0]);
			if (newItem == null)
			{
				const string message = "Invalid receipt number";
				MessageBox.Show(message);
			}
			else
			{
				pool.Add(newItem);
				for (int i = 1; i < list.Count; i++)
				{
					pool.Add(USCISStatus.Track(list[i]));
				}
			}
			this.Cursor = Cursors.Default;
			box.Close();
		}

		private void groupBox2_Enter(object sender, EventArgs e)
		{

		}

		private void button2_Click(object sender, EventArgs e)
		{
			// refresh all
			this.Cursor = Cursors.WaitCursor;
			pool.RefreshAll();
			this.Cursor = Cursors.Default;
			dataGridView1.Update();
		}

		private void Form1_FormClosing(object sender, FormClosingEventArgs e)
		{
			pool.ToFile("receipt.pool");
		}

		private void Form1_Resize(object sender, EventArgs e)
		{
			Size size = this.Size;
			this.groupBox1.Location = new Point(12, 12);
			this.groupBox1.Size = new Size(size.Width - 36, 120);
			this.groupBox2.Location = new Point(12, 105);
			this.groupBox2.Size = new Size(size.Width - 36, size.Height - 120 - 48);
			//this.textBox1.Location = new Point(size.Width - 48 - this.textBox1.Size.Width, 34);
			//this.button1.Location = new Point(size.Width - 48 - this.button1.Size.Width, 60);
			//this.button2.Location = new Point(size.Width - 48 - this.button2.Size.Width, 16);
			this.dataGridView1.Location = new Point(12, 48);
			this.dataGridView1.Size = new Size(this.groupBox2.Width - 24, this.groupBox2.Height - 64);
		}

		private void button4_Click(object sender, EventArgs e)
		{
			const string about = "About";
			const string message = "Fan Deng (2012)\nAll rights reserved.";
			MessageBox.Show(message, about);
		}

		private void button3_Click(object sender, EventArgs e)
		{
			var selected = dataGridView1.SelectedRows;
			if (selected.Count == 0)
			{
				MessageBox.Show("To select a record, click the leftmost column.");
			}
			else
			{
				for (int i = 0; i < selected.Count; i++)
				{
					pool.Remove(selected[i].Index);
				}
			}
		}

		private void textBox1_TextChanged(object sender, EventArgs e)
		{

		}
	}
}
